unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, StdCtrls, RzEdit, RzButton, RzRadChk, Mask,
  rtcInfo, rtcConn, rtcTcpSrv,FormEx_View,rtcCliPool, rtcThrPool;

type
  TKzTcpRcvJob=class(TRtcJob)
    ReadedTxt:string;
    conn:TRtcConnection;
    function Run(Thr:TRtcThread):boolean; override;
    procedure Kill; override;
  end;
  
  TForm1 = class(TFormExView)
    Tool_1: TRzToolbar;
    Memo_1: TRzMemo;
    Edit_Addr: TRzEdit;
    Edit_Port: TRzEdit;
    Chkb_Actv: TRzCheckBox;
    RtcTcpServer1: TRtcTcpServer;
    procedure Chkb_ActvClick(Sender: TObject);
    procedure RtcTcpServer1DataReceived(Sender: TRtcConnection);
  private
  protected
    procedure SetInitialize;override;
    procedure SetCommParams;override;
    procedure SetGridParams;override;
    procedure SetComboItems;override;
    procedure TryFreeAndNil;override;
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils,Class_KzDebug;

{$R *.dfm}

procedure TForm1.Chkb_ActvClick(Sender: TObject);
begin
  if Chkb_Actv.Checked then
  begin
    RtcTcpServer1.ServerAddr:=Trim(Edit_Addr.Text);
    RtcTcpServer1.ServerPort:=Trim(Edit_Port.Text);
    RtcTcpServer1.Listen();  
  end else
  begin
    RtcTcpServer1.StopListen;
  end;
end;

procedure TForm1.RtcTcpServer1DataReceived(Sender: TRtcConnection);
{var
  TextA:string;
  ListA:TStringList;}
var
  KzTcpRcvJob:TKzTcpRcvJob;
begin
  if Sender.inMainThread then
  begin
    KzTcpRcvJob:=TKzTcpRcvJob.Create;
      
    KzTcpRcvJob.ReadedTxt:='';
    KzTcpRcvJob.conn:=Sender;
    Sender.PostJob(KzTcpRcvJob);
  end else
  begin
    Sender.Sync(RtcTcpServer1DataReceived);
  end;
  {if Sender.inMainThread then
  begin
    TextA:=Sender.Read;
    Sender.Info.asString['RECV']:=Sender.Info.asString['RECV']+TEXTA;
    TextA:=Sender.Info.asString['RECV'];
    Memo_1.Lines.Add(Format('readcount%D:%D:%D',[Sender.DataIn,Length(TextA),Length(TextA)]));

    ListA:=TStringList.Create;
    ListA.LoadFromFile(TKzUtils.ExePath+'8620-8621-to-6201.4003064335 out2in.dmp');
    Sender.Write(ListA.Strings[0]);
    FreeAndNil(ListA);
  end else
  begin
    Sender.Sync(RtcTcpServer1DataReceived);
  end;}
end;

procedure TForm1.SetComboItems;
begin
  inherited;

end;

procedure TForm1.SetCommParams;
begin
  inherited;
  Memo_1.Lines.Clear;
  Edit_Addr.Text:='localhost';
  Edit_Port.Text:='907';
end;

procedure TForm1.SetGridParams;
begin
  inherited;

end;

procedure TForm1.SetInitialize;
begin
  inherited;

end;

procedure TForm1.TryFreeAndNil;
begin
  inherited;

end;

{ TKzTcpRcvJob }

procedure TKzTcpRcvJob.Kill;
begin
  inherited;
  KzDebug.FileFmt('allreaded:%D',[Length(ReadedTxt)]);
  Form1.Memo_1.Lines.Add(Format('allreaded:%D',[Length(ReadedTxt)]));
  Free;
end;

function TKzTcpRcvJob.Run(Thr: TRtcThread): boolean;
var
  LengA:Integer;
begin
  ReadedTxt:=ReadedTxt+conn.Read;

  LengA:=StrToIntDef(Copy(ReadedTxt,1,6),0);
  if LengA+30 = Length(ReadedTxt) then
  begin
    Result:=True;
  end;
end;

end.
